﻿using System;
using System.Security.Principal;
using System.ServiceModel;
using System.Threading.Tasks;

namespace Tiscs.Practices.Web.Services.Client
{
    /// <summary>
    /// 身份验证客户端定义接口。
    /// </summary>
    /// <typeparam name="T">用户信息类的类型。</typeparam>
    [ServiceContract(Name = AuthenticationServiceResource.ServiceName, Namespace = AuthenticationServiceResource.ServiceNamespace)]
    public interface IAuthenticationServiceClient<T> where T : IPrincipal
    {
        /// <summary>
        /// 开始获取用户操作。
        /// </summary>
        /// <param name="callback">异步操作回调。</param>
        /// <param name="asyncState">用户状态。</param>
        /// <returns>异步操作结果。</returns>
        [OperationContract(AsyncPattern = true,
            Name = AuthenticationServiceResource.GetUserName,
            Action = AuthenticationServiceResource.GetUserAction,
            ReplyAction = AuthenticationServiceResource.GetUserReplyAction)]
        IAsyncResult BeginGetUser(AsyncCallback callback, object asyncState);

        /// <summary>
        /// 完成获取用户操作。
        /// </summary>
        /// <param name="result">异步操作结果。</param>
        /// <returns>用户信息实例。</returns>
        T EndGetUser(IAsyncResult result);

        /// <summary>
        /// 开始用户登录操作。
        /// </summary>
        /// <param name="userName">用户名。</param>
        /// <param name="password">密码。</param>
        /// <param name="customCredential">自定义凭据。</param>
        /// <param name="isPersistent">是否持久登录。</param>
        /// <param name="callback">异步操作回调。</param>
        /// <param name="asyncState">用户状态。</param>
        /// <returns>异步操作结果。</returns>
        [OperationContract(AsyncPattern = true,
            Name = AuthenticationServiceResource.SignInName,
            Action = AuthenticationServiceResource.SignInAction,
            ReplyAction = AuthenticationServiceResource.SignInReplyAction)]
        IAsyncResult BeginSignIn(string userName, string password, string customCredential, bool isPersistent, AsyncCallback callback, object asyncState);

        /// <summary>
        /// 完成用户登录操作。
        /// </summary>
        /// <param name="result">异步操作结果。</param>
        /// <returns>用户信息实例。</returns>
        T EndSignIn(IAsyncResult result);

        /// <summary>
        /// 开始用户注销操作。
        /// </summary>
        /// <param name="callback">异步操作回调。</param>
        /// <param name="asyncState">用户状态。</param>
        /// <returns>异步操作结果。</returns>
        [OperationContract(AsyncPattern = true,
            Name = AuthenticationServiceResource.SignOutName,
            Action = AuthenticationServiceResource.SignOutAction,
            ReplyAction = AuthenticationServiceResource.SignOutReplyAction)]
        IAsyncResult BeginSignOut(AsyncCallback callback, object asyncState);

        /// <summary>
        /// 完成用户注销操作。
        /// </summary>
        /// <param name="result">异步操作结果。</param>
        /// <returns>用户信息实例。</returns>
        T EndSignOut(IAsyncResult result);

        /// <summary>
        /// 开始更新用户操作。
        /// </summary>
        /// <param name="user">用户信息实例。</param>
        /// <param name="callback">异步操作回调。</param>
        /// <param name="asyncState">用户状态。</param>
        /// <returns>异步操作结果。</returns>
        [OperationContract(AsyncPattern = true,
            Name = AuthenticationServiceResource.UpdateUserName,
            Action = AuthenticationServiceResource.UpdateUserAction,
            ReplyAction = AuthenticationServiceResource.UpdateUserReplyAction)]
        IAsyncResult BeginUpdateUser(T user, AsyncCallback callback, object asyncState);

        /// <summary>
        /// 完成更新用户操作。
        /// </summary>
        /// <param name="result">异步操作结果。</param>
        void EndUpdateUser(IAsyncResult result);

#if SERVER || DESKTOP

        /// <summary>
        /// 获取用户。
        /// </summary>
        /// <returns>用户信息实例。</returns>
        [OperationContract(AsyncPattern = false,
            Name = AuthenticationServiceResource.GetUserName,
            Action = AuthenticationServiceResource.GetUserAction,
            ReplyAction = AuthenticationServiceResource.GetUserReplyAction)]
        T GetUser();

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName">用户名。</param>
        /// <param name="password">密码。</param>
        /// <param name="customCredential">自定义凭据。</param>
        /// <param name="isPersistent">是否持久登录。</param>
        /// <returns>用户信息实例。</returns>
        [OperationContract(AsyncPattern = false,
            Name = AuthenticationServiceResource.SignInName,
            Action = AuthenticationServiceResource.SignInAction,
            ReplyAction = AuthenticationServiceResource.SignInReplyAction)]
        T SignIn(string userName, string password, string customCredential, bool isPersistent);

        /// <summary>
        /// 注销登录。
        /// </summary>
        /// <returns>用户信息实例。</returns>
        [OperationContract(AsyncPattern = false,
            Name = AuthenticationServiceResource.SignOutName,
            Action = AuthenticationServiceResource.SignOutAction,
            ReplyAction = AuthenticationServiceResource.SignOutReplyAction)]
        T SignOut();

        /// <summary>
        /// 更新用户。
        /// </summary>
        /// <param name="user">用户信息实例。</param>
        [OperationContract(AsyncPattern = false,
            Name = AuthenticationServiceResource.UpdateUserName,
            Action = AuthenticationServiceResource.UpdateUserAction,
            ReplyAction = AuthenticationServiceResource.UpdateUserReplyAction)]
        void UpdateUser(T user);

#endif
    }
}